import pandas as pd

# ==========查看数据==========
# 用pandas库读取数据
train_data = pd.read_csv('input/sf-crime/train.csv.zip', parse_dates=['Dates'])
test_data = pd.read_csv('input/sf-crime/test.csv.zip', parse_dates=['Dates'])
# 查看两个数据集的基本信息
# train_data.info()
# test_data.info()
# # 显示前五行数据
# print(train_data[:5])
# print(test_data[:5])

# ==========数据可视化分析==========
# 旧金山市，2003年至2015年，各犯罪种类数量的直方图：，旧金山市的犯罪集中在靠前的几类中。
import seaborn as sns
import matplotlib.pyplot as plt

cate_group = train_data.groupby(by='Category').size()
cat_num = len(cate_group.index)
cate_group.sort_values(ascending=False,inplace=True)
cate_group.plot(kind='bar',logy=True,color=sns.color_palette('coolwarm',cat_num))
plt.title('No. of Crime types',fontsize=20)
plt.show()

# 此图显示的是犯罪数量与年份的关系，但是2015年出现了一次暴跌，根据观察，发现2015年的记录只到3月份，暂时排除2015年的记录，修改程序得到新的折线图（右）
# train_data['date'] = pd.to_datetime(train_data['Dates'])
# train_data['year'] = train_data.date.dt.year
# train_data['month'] = train_data.date.dt.month
# train_data['day'] = train_data.date.dt.day
# train_data['hour'] = train_data.date.dt.hour
# train_data['minute'] = train_data.date.dt.minute

# year_group = train_data.groupby('year').size()
# #plt.plot(year_group, 'ks-')
# plt.plot(year_group.index[:-1],year_group[:-1],'ks-')
# # 2015年不完整
# plt.xlabel('year')
# plt.title('No. of crimes by year', fontsize=20)
# plt.show()

#top10 = list(cate_group.index[:10])
# tmp = train_data[train_data['Category'].isin(top10)]
# mon_g = tmp.groupby(['Category','month']).size()
# mon_g = mon_g.unstack()
# for i in range(10):
#     mon_g.iloc[i] = mon_g.iloc[i]/mon_g.sum(axis=1)[i]
# mon_g.T.plot(figsize=(12,6),style='o-')
# plt.show()

#top6 = list(cate_group.index[:6])
# tmp = train_data[train_data['Category'].isin(top6)]
# hou_g = tmp.groupby(['Category','hour']).size()
# hou_g = hou_g.unstack()
# hou_g.T.plot(figsize=(12,6),style='o-')
# plt.show()

# wkm = {
#     'Monday': 0,
#     'Tuesday': 1,
#     'Wednesday': 2,
#     'Thursday': 3,
#     'Friday': 4,
#     'Saturday': 5,
#     'Sunday': 6
# }
# train_data['DayOfWeek'] = train_data['DayOfWeek'].apply(lambda x: wkm[x])
# tmp = train_data[train_data['Category'].isin(top6)]
# wee_group = tmp.groupby(['Category', 'DayOfWeek']).size()
# wee_group = wee_group.unstack()
# wee_group.T.plot(figsize=(12, 6), style='o-')
# plt.xticks([0, 1, 2, 3, 4, 5, 6],
#            ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'])
# plt.show()

# dis_group = train_data.groupby(by='PdDistrict').size()
# dis_num = len(dis_group.index)
# dis_group.sort_values(ascending=False, inplace=True)
# dis_group.plot(kind='bar',
#                fontsize=10,
#                color=sns.color_palette('coolwarm', dis_num))
# plt.title('No. of crimes by district', fontsize=20)
# plt.show()

# train_data['block'] = train_data['Address'].apply(
#     lambda x: 1 if 'block' in x.lower() else 0)
# tmp = train_data[train_data['Category'].isin(top10)]
# blo_group = tmp.groupby(['Category', 'block']).size()
# blo_group.unstack().T.plot(kind='bar', figsize=(12, 6), rot=45)
# plt.xticks([0, 1], ['no block', 'block'])
# plt.show()

# xy_group = pd.concat([train_data.X, train_data.Y], axis=1)
# xy_group = xy_group.drop(xy_group[xy_group.Y > 50].index)
# #存在66个（-120.5,90.0）点
# xy_group.plot(kind='scatter', x='X', y='Y')
# plt.xlabel('X')
# plt.ylabel('Y')
# plt.show()

# img = plt.imread('data/map.png')
# #PS:此图从openstreetmap截下来的
# dpi = 100
# height, width, depth = img.shape
# plt.figure(figsize=(width / dpi, height / dpi))
# plt.imshow(img)
# plt.axis('off')
# plt.show()
# plt.figure(figsize=(width / dpi, height / dpi))
# plt.hist2d(xy_group.X.values, xy_group.Y.values, bins=40, cmap='Reds')
# plt.show()


if __name__ == '__main__':
    print()
